package com.hdoit.apollo.web.stock.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.common.enums.stock.InStockType;
import com.hdoit.apollo.logical.inStock.model.InStock;
import com.hdoit.apollo.logical.inStock.model.InStockShow;
import com.hdoit.apollo.logical.inStock.service.IInStockService;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;

@Controller
@RequestMapping(value = "/api/v1/inStock/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/inStock")
public class InStockApi extends BaseApi{

	@Autowired
	private IInStockService inStockService;
	
	private final String saveInStockApi="saveInStock";
	
    @RequestMapping(value = saveInStockApi)
    @ApiOperation(notes = saveInStockApi, httpMethod = "POST", value = "保存入库信息")
    public @ResponseBody String saveInStock(InStock inStock,String inStockShowsJson){
    	InStock inStockPan = inStockService.queryModel(inStock.getInStockId());
    	if(inStockPan==null){
    		inStock.setType(InStockType.batchInStock);
    		String timestamp = DateUtils.convertDate2String("yyyyMMddHHmmss", DateUtils.getCurrent());
    		inStock.setInStockNo("IN-"+timestamp);
    		inStock.setCreateDate(DateUtils.getCurrent());
    		inStock.setCreateUserId(getLoginId());
    		inStock.setCreateUserName(getLoginName());
            inStock.setDeleteMark(YesOrNoInteger.no);
            inStock.setModelStatus(ModelStatus.add);
    	}else{
    		inStock.setModifyUserId(getLoginId());
    		inStock.setModifyUserName(getLoginName());
    		inStock.setModifyDate(DateUtils.getCurrent());
    		inStock.setModelStatus(ModelStatus.update);
    	}
    	
    	@SuppressWarnings("unchecked")
		List<InStockShow> inStockShows = (List<InStockShow>) JsonUtils.readValues(inStockShowsJson, List.class, InStockShow.class);
    	
    	ResponseModel rm=new ResponseModel();
    	if(!inStockService.applyInStock(inStock,inStockShows)){
    		rm.setStatus(ResponseStatus.dataError);
    	}
    	return rm.toJson();
    }

}
